<?php

namespace easydis\APSBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\Session;
use easydis\APSBundle\Entity\Planifier;
use easydis\APSBundle\Entity\PlageHoraire;
use easydis\APSBundle\Entity\Poste;
use easydis\APSBundle\Form\PlanifierType;
use easydis\APSBundle\Form\PlagehoraireType;
use easydis\APSBundle\Form\PosteType;
use \easydis\APSBundle\Entity\EventLocal;
use \DateInterval;

class PlanningController extends Controller
{

    private function interTabObj($list_salarie, $list_planifier) {
        
        $tabresult = array(array());
        $i = 0;
        foreach($list_salarie as $salarie) {
            $test = false;
            foreach($list_planifier as $plan) {
                if($plan->getSalarie()->getId() == $salarie->getId()) {
                    $test = true;
                    break;
                }
            }
            if ($test == false) {
                $tabresult[$i][0] = $salarie->getNom() . ' ' . $salarie->getPrenom() . ' ' . $salarie->getPrenom2() . ' ' . $salarie->getPrenom3();
                $tabresult[$i][1] = $salarie;
                $i = $i + 1;
            }
        }
        
        return $tabresult;
    }
    
    private function isInTabEffectif($list_salarie, $sal) {
        
        $test = 0;
        foreach($list_salarie as $salarie) {
            if($salarie->getId() == $sal->getId())
            {
                $test = 1;
                break;
            }
        }
        
        return $test;
    }
    
    private function isinTabPoste($tab, $value) {
        
        foreach($tab as $poste) {
            if($poste->getId() == $value)
                return 1;
        }
        return 0;
    }
   
    public function redirectAction($gs, $mode, $idsal, $date)
    {
       $response = $this->forward('easydisAPSBundle:Homepage:index');
        
       $request = $this->getRequest();
       if($request->getMethod() == 'POST')
       {
          $date = ($request->request->get('date')) ? $request->request->get('date') : $date;
          $gs = ($request->request->get('idgs')) ? $request->request->get('idgs') : $gs;
       }
       if($mode == 'view'){
            $response = $this->forward('easydisAPSBundle:Planning:view', array(
            'date'  => $date,
            'gs' => $gs,
            ));
        }
        if($mode == 'modif_unit'){
            $response = $this->forward('easydisAPSBundle:Planning:modif_unitaire', array(
            'date'  => $date,
            'gs' => $gs,
            'idsal' => $idsal
            ));
        } 
        return $response;
    }
    
    public function viewAction($date, $gs, $mode = 'full')
    {
        $em = $this->getDoctrine()->getManager();

        $repository_salarie = $em->getRepository('easydisAPSBundle:Salarie');
        $repository_planifier = $em->getRepository('easydisAPSBundle:Planifier');
        $repository_poste = $em->getRepository('easydisAPSBundle:Poste');
        
        // Debut gestion des dates
        // Calcul de la date de debut et de fin de la semaine à partir de $date
        $dateO = date_create_from_format ('d/m/Y', $date);
        $dateDebut = clone $dateO;
        $JourDsSemaine = $dateO->format('N');
        $ecart = -(1-$JourDsSemaine);
        $interval = new DateInterval('P'.$ecart.'D');
        $interval->invert = 1;
        $dateDebut->add($interval);
        $dateFin = clone $dateDebut;
        $increment = new DateInterval('P6D'); 
        $dateFin->add($increment);
        $increment = new DateInterval('P1D');
        $datetmp = clone $dateDebut;
        $tabDates = array();
        $i = 0;
        while($datetmp <= $dateFin) {
            $tabDates[$i] = $datetmp->format("j/m");
            $datetmp->add($increment);
            $i = $i + 1;
        }
        $dateFin->add($increment);
        $increment->invert = 1;
        $dateDebut->add($increment);
        // Fin gestion des dates
        
        $list_effectif = $repository_salarie->get_list_effectif($gs, 'groupe_service_visible');
        $list_postes = $repository_poste->get_postes($gs, 'groupe_service');
        
        $tabResult2 = array(array(array()));
        $tabResult3 = array(array(array()));
        $tabResult2[0][0][0] = null;
        $tabResult3[0][0][0] = null;
        $numsalarie = 0;
        $numsalarie2 = 0;
        $enteteligne = "";
        $lastservice = "";
        // retourne tout les salarie affectés a un poste rataché a l'une des section du gs donné dans une plage de temps donnée
        $planifier = $repository_planifier->get_list_effectif_planifier_periode($dateDebut, $dateFin, $gs, 'groupe_service');
        // retourne tout les salarie rataché directement a l'une des section du gs donné sur une plage de temps donné
        $planifier2 = $repository_planifier->get_list_effectif_planifier_periode($dateDebut, $dateFin, $gs, 'groupe_service2');
        foreach($planifier2 as $element_p2) {
            $test = false;
            foreach($planifier as $element_p) {
                if($element_p2->getSalarie()->getId() == $element_p->getSalarie()->getId()) {
                    $test = true;
                    break;
                }
            }
            if($test == false)
               $nb = array_push($planifier, $element_p2);
        }
        foreach($planifier as $attrib) {
            $test = null;
            $test2 = null;
            $enteteligne = $attrib->getSalarie()->getNom() . ' ' . $attrib->getSalarie()->getPrenom() . ' ' . $attrib->getSalarie()->getPrenom2() . ' ' . $attrib->getSalarie()->getPrenom3();        
            /* INIIALISATION DU TABLEAU DE RESULTATS */
            // contien les postes
            $tabResult2[$numsalarie][0][0] = null;
            $tabResult2[$numsalarie][0][1] = null;
            $tabResult2[$numsalarie][1][1] = null;
            $tabResult2[$numsalarie][2][1] = null;
            $tabResult2[$numsalarie][3][1] = null;
            $tabResult2[$numsalarie][4][1] = null;
            $tabResult2[$numsalarie][5][1] = null;
            $tabResult2[$numsalarie][6][1] = null;
            
            // contien les plages horaires
            $tabResult2[$numsalarie][0][2] = null;
            $tabResult2[$numsalarie][1][2] = null;
            $tabResult2[$numsalarie][2][2] = null;
            $tabResult2[$numsalarie][3][2] = null;
            $tabResult2[$numsalarie][4][2] = null;
            $tabResult2[$numsalarie][5][2] = null;
            $tabResult2[$numsalarie][6][2] = null;
            
            // contien les couleurs des postes
            $tabResult2[$numsalarie][0][3] = null;
            $tabResult2[$numsalarie][1][3] = null;
            $tabResult2[$numsalarie][2][3] = null;
            $tabResult2[$numsalarie][3][3] = null;
            $tabResult2[$numsalarie][4][3] = null;
            $tabResult2[$numsalarie][5][3] = null;
            $tabResult2[$numsalarie][6][3] = null;
            
             // contien les postes
            $tabResult3[$numsalarie2][0][0] = null;
            $tabResult3[$numsalarie2][0][1] = null;
            $tabResult3[$numsalarie2][1][1] = null;
            $tabResult3[$numsalarie2][2][1] = null;
            $tabResult3[$numsalarie2][3][1] = null;
            $tabResult3[$numsalarie2][4][1] = null;
            $tabResult3[$numsalarie2][5][1] = null;
            $tabResult3[$numsalarie2][6][1] = null;
            
            // contien les plages horaires
            $tabResult3[$numsalarie2][0][2] = null;
            $tabResult3[$numsalarie2][1][2] = null;
            $tabResult3[$numsalarie2][2][2] = null;
            $tabResult3[$numsalarie2][3][2] = null;
            $tabResult3[$numsalarie2][4][2] = null;
            $tabResult3[$numsalarie2][5][2] = null;
            $tabResult3[$numsalarie2][6][2] = null;
            
            // contien les couleurs des postes
            $tabResult3[$numsalarie2][0][3] = null;
            $tabResult3[$numsalarie2][1][3] = null;
            $tabResult3[$numsalarie2][2][3] = null;
            $tabResult3[$numsalarie2][3][3] = null;
            $tabResult3[$numsalarie2][4][3] = null;
            $tabResult3[$numsalarie2][5][3] = null;
            $tabResult3[$numsalarie2][6][3] = null;
            /* FIN INITIALISATION */
            
            if($lastservice != $attrib->getSalarie()->getSection()->getService()->getLib() || $lastservice == "") {
                $tabResult2[$numsalarie][0][0] = array($attrib->getSalarie()->getSection()->getService()->getLib(), 'Lib');
                $numsalarie = $numsalarie + 1;
                $numsalarie2 = $numsalarie2 + 1;
                 
                 /* INIIALISATION DU TABLEAU DE RESULTATS */
                // contien les postes
                $tabResult2[$numsalarie][0][0] = null;
                $tabResult2[$numsalarie][0][1] = null;
                $tabResult2[$numsalarie][1][1] = null;
                $tabResult2[$numsalarie][2][1] = null;
                $tabResult2[$numsalarie][3][1] = null;
                $tabResult2[$numsalarie][4][1] = null;
                $tabResult2[$numsalarie][5][1] = null;
                $tabResult2[$numsalarie][6][1] = null;

                // contien les plages horaires
                $tabResult2[$numsalarie][0][2] = null;
                $tabResult2[$numsalarie][1][2] = null;
                $tabResult2[$numsalarie][2][2] = null;
                $tabResult2[$numsalarie][3][2] = null;
                $tabResult2[$numsalarie][4][2] = null;
                $tabResult2[$numsalarie][5][2] = null;
                $tabResult2[$numsalarie][6][2] = null;

                // contien les couleurs des postes
                $tabResult2[$numsalarie][0][3] = null;
                $tabResult2[$numsalarie][1][3] = null;
                $tabResult2[$numsalarie][2][3] = null;
                $tabResult2[$numsalarie][3][3] = null;
                $tabResult2[$numsalarie][4][3] = null;
                $tabResult2[$numsalarie][5][3] = null;
                $tabResult2[$numsalarie][6][3] = null;

                 // contien les postes
                $tabResult3[$numsalarie2][0][0] = null;
                $tabResult3[$numsalarie2][0][1] = null;
                $tabResult3[$numsalarie2][1][1] = null;
                $tabResult3[$numsalarie2][2][1] = null;
                $tabResult3[$numsalarie2][3][1] = null;
                $tabResult3[$numsalarie2][4][1] = null;
                $tabResult3[$numsalarie2][5][1] = null;
                $tabResult3[$numsalarie2][6][1] = null;

                // contien les plages horaires
                $tabResult3[$numsalarie2][0][2] = null;
                $tabResult3[$numsalarie2][1][2] = null;
                $tabResult3[$numsalarie2][2][2] = null;
                $tabResult3[$numsalarie2][3][2] = null;
                $tabResult3[$numsalarie2][4][2] = null;
                $tabResult3[$numsalarie2][5][2] = null;
                $tabResult3[$numsalarie2][6][2] = null;

                // contien les couleurs des postes
                $tabResult3[$numsalarie2][0][3] = null;
                $tabResult3[$numsalarie2][1][3] = null;
                $tabResult3[$numsalarie2][2][3] = null;
                $tabResult3[$numsalarie2][3][3] = null;
                $tabResult3[$numsalarie2][4][3] = null;
                $tabResult3[$numsalarie2][5][3] = null;
                $tabResult3[$numsalarie2][6][3] = null;
                /* FIN INITIALISATION */
            }
            $semaine = $repository_planifier->get_planning($dateDebut, $dateFin, $attrib->getSalarie()->getId());
            foreach($semaine as $jour) {
                // Permet de faire corespondre les dates des deux tableaux
                $col = 0;
                while($col <= 6) {
                    if ($tabDates[$col] == $jour->getDate_eng()->format("j/m"))
                        break;
                    $col = $col + 1; 
                }
                if($col <= 6) {
                    $test = false;
                    if($tabDates[$col] == $jour->getDate_eng()->format("j/m")) {
                        $test = true;
                        $origine = "";
                        $etranger = 0;
                        if($this->isInTabEffectif($list_effectif, $attrib->getSalarie()) == 0) {
                            $origine = "[".$attrib->getSalarie()->getSection()->getService()->getLib()."] ";
                            $etranger = 1;
                        }
                        if($this->isinTabPoste($list_postes, $jour->getPoste()->getId()) == 1) {
                            $tabResult2[$numsalarie][0][0] = array($origine . $enteteligne, $attrib->getSalarie());
                            $tabResult2[$numsalarie][$col][1] = ($tabResult2[$numsalarie][$col][1]) ? ($tabResult2[$numsalarie][$col][1]."/".$jour->getPoste()->getLib()) : $jour->getPoste()->getLib();
                            $tabResult2[$numsalarie][$col][2] = ($tabResult2[$numsalarie][$col][2]) ? ($tabResult2[$numsalarie][$col][2]."/".$jour->getPlageHoraire()->getLib()) : $jour->getPlageHoraire()->getLib();
                            $tabResult2[$numsalarie][$col][3] = ($tabResult2[$numsalarie][$col][3]) ? ($tabResult2[$numsalarie][$col][3]."/".$jour->getPoste()->getColor()) : $jour->getPoste()->getColor();
                        }
                        if($this->isInTabEffectif($list_effectif, $attrib->getSalarie()) == 1 && $this->isinTabPoste($list_postes, $jour->getPoste()->getId()) == 0) {
                            $tabResult3[$numsalarie2][0][0] = array($attrib->getSalarie()->getNom() . ' ' . $attrib->getSalarie()->getPrenom() . ' ' . $attrib->getSalarie()->getPrenom2() . ' ' . $attrib->getSalarie()->getPrenom3(), $attrib->getSalarie());
                            $tabResult3[$numsalarie2][$col][1] = $jour->getPoste()->getLib();
                            $tabResult3[$numsalarie2][$col][2] = $jour->getPlageHoraire()->getLib();
                            $tabResult3[$numsalarie2][$col][3] = $jour->getPoste()->getColor();
                            $tabResult2[$numsalarie][$col][1] = '*';
                            $tabResult2[$numsalarie][$col][2] = '*';
                            $tabResult2[$numsalarie][$col][3] = '#000';
                            $test2 = true;
                        }
                    }
                }
            }
            if ($test) {
                $numsalarie = $numsalarie + 1;
                if($test2)
                    $numsalarie2 = $numsalarie2 + 1;
            }
            $test2 = false;
            $lastservice = $attrib->getSalarie()->getSection()->getService()->getLib();
        }
        
        $list_salarie = $repository_salarie->get_list_effectif($gs, 'groupe_service_visible');
        $tabResult = $this->interTabObj($list_salarie, $planifier);
        
        $tabrecap = array(array());
        $row = 0;
        // Tableau recapitulatif
        $increment->invert = 0;
        foreach($list_postes as $poste) {
            $col = 0;
            $tabrecap[$row][$col] = $poste->getLib();
            $col = 1;
            $datetmp2 = clone $dateDebut;
            $datetmp2->add($increment);
            foreach($tabDates as $datetmp) {
                $count_poste = $repository_poste->count_postes($poste, $datetmp2->format('d/m/Y'));
                $sum = $repository_poste->sumh_postes($poste, $datetmp2->format('d/m/Y'));
                $sum = ($sum )? $sum : 0;
                $tabrecap[$row][$col] = $count_poste." [".$sum."h]";
                $datetmp2->add($increment);
                $col++;
            }
            $row++;
        }
    
        // Gestion de la version imprimable
        if($mode == 'full') {
            $route = 'easydisAPSBundle:Planning:planning.html.twig';
        } else if($mode =='imprimable_reduit') {
            $route = 'easydisAPSBundle:Planning:planning_imprimable_reduit.html.twig';
        } else {
            $route = 'easydisAPSBundle:Planning:planning_imprimable.html.twig';
        }
        
        return $this->render($route, array('num_semaine' => $dateDebut->add($increment)->format('W'), 'dispo' => $tabResult, 'tab2' => $tabResult3, 'planning' => $tabResult2, 'tabdates' => $tabDates, 'gs' => $gs, 'date' => $date, 'recap' => $tabrecap));
    }
    
    /*
     * 
     * MODIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIFFFFFFFFFFFFFFFFFFFFFFFF
     * 
     */
    public function modif_unitaireAction($date, $gs, $idsal)
    {
        $em = $this->getDoctrine()->getManager();

        $repository_salarie = $em->getRepository('easydisAPSBundle:Salarie');
        $repository_planifier = $em->getRepository('easydisAPSBundle:Planifier');
        $repository_poste = $em->getRepository('easydisAPSBundle:Poste');
        $repository_plagehoraire = $em->getRepository('easydisAPSBundle:PlageHoraire');
        $repository_groupeservice = $em->getRepository('easydisAPSBundle:GroupeService');
        
        
        // Calcul de la date de debut et de fin de la semaine à partir de $date
        $date = date_create_from_format ('d/m/Y', $date);
        $dateDebut = clone $date;
        
        $JourDsSemaine = $date->format('N');
        $ecart = -(1-$JourDsSemaine);
        $interval = new DateInterval('P'.$ecart.'D');
        $interval->invert = 1;
        $dateDebut->add($interval);
        $dateFin = clone $dateDebut;
        $increment = new DateInterval('P6D'); 
        $dateFin->add($increment);
        
        // Création du tableau des dates pour les entêtes
        $increment = new DateInterval('P1D');
        $datetmp = clone $dateDebut;
        $tabDates = array();
        $i = 0;
        while($datetmp <= $dateFin) {
            $tabDates[$i] = $datetmp->format("j/m");
            $datetmp->add($increment);
            $i = $i + 1;
        }
        $dateFin->add($increment);
        $datetmp = clone $dateDebut;
        $increment->invert = 1;
        $datetmp->add($increment);
        /* Fin gestion des dates */
        
        $list_effectif = $repository_salarie->get_list_effectif($gs, 'groupe_service');
        $list_postes = $repository_poste->get_postes($gs, 'groupe_service');
        $list_ph = $repository_plagehoraire->get_plagehoraires($gs);
        $planifier = $repository_planifier->get_list_effectif_planifier_periode($datetmp, $dateFin, $idsal, 'salarie');
        $tabResult2 = array(array(array()));
        $tabResult3 = array(array(array()));
        $tabResult2[0][0][0] = null;
        $tabResult3[0][0][0] = null;
        $numsalarie = 0;
        $numsalarie2 = 0;
        $test = null;
        $test2 = null;
        $session = new Session();
        foreach($planifier as $attrib) {
            if ($attrib->getSalarie()->getId() == $idsal)
            {
                /* INIIALISATION DU TABLEAU DE RESULTATS */
                // contien les postes
                $tabResult2[$numsalarie][0][0] = null;
                $tabResult2[$numsalarie][0][1] = null;
                $tabResult2[$numsalarie][1][1] = null;
                $tabResult2[$numsalarie][2][1] = null;
                $tabResult2[$numsalarie][3][1] = null;
                $tabResult2[$numsalarie][4][1] = null;
                $tabResult2[$numsalarie][5][1] = null;
                $tabResult2[$numsalarie][6][1] = null;

                // contien les plages horaires
                $tabResult2[$numsalarie][0][2] = null;
                $tabResult2[$numsalarie][1][2] = null;
                $tabResult2[$numsalarie][2][2] = null;
                $tabResult2[$numsalarie][3][2] = null;
                $tabResult2[$numsalarie][4][2] = null;
                $tabResult2[$numsalarie][5][2] = null;
                $tabResult2[$numsalarie][6][2] = null;

                // contien les couleurs des postes
                $tabResult2[$numsalarie][0][3] = null;
                $tabResult2[$numsalarie][1][3] = null;
                $tabResult2[$numsalarie][2][3] = null;
                $tabResult2[$numsalarie][3][3] = null;
                $tabResult2[$numsalarie][4][3] = null;
                $tabResult2[$numsalarie][5][3] = null;
                $tabResult2[$numsalarie][6][3] = null;

                // contien les postes
                $tabResult3[$numsalarie2][0][0] = null;
                $tabResult3[$numsalarie2][0][1] = null;
                $tabResult3[$numsalarie2][1][1] = null;
                $tabResult3[$numsalarie2][2][1] = null;
                $tabResult3[$numsalarie2][3][1] = null;
                $tabResult3[$numsalarie2][4][1] = null;
                $tabResult3[$numsalarie2][5][1] = null;
                $tabResult3[$numsalarie2][6][1] = null;

                // contien les plages horaires
                $tabResult3[$numsalarie2][0][2] = null;
                $tabResult3[$numsalarie2][1][2] = null;
                $tabResult3[$numsalarie2][2][2] = null;
                $tabResult3[$numsalarie2][3][2] = null;
                $tabResult3[$numsalarie2][4][2] = null;
                $tabResult3[$numsalarie2][5][2] = null;
                $tabResult3[$numsalarie2][6][2] = null;

                // contien les couleurs des postes
                $tabResult3[$numsalarie2][0][3] = null;
                $tabResult3[$numsalarie2][1][3] = null;
                $tabResult3[$numsalarie2][2][3] = null;
                $tabResult3[$numsalarie2][3][3] = null;
                $tabResult3[$numsalarie2][4][3] = null;
                $tabResult3[$numsalarie2][5][3] = null;
                $tabResult3[$numsalarie2][6][3] = null;
                /* FIN INITIALISATION */
                $semaine = $repository_planifier->get_planning($datetmp, $dateFin, $attrib->getSalarie()->getId());
                foreach($semaine as $jour) {
                    // Permet de faire corespondre les dates des deux tableaux
                    $col = 0;
                    while($col <= 6) {
                        if ($tabDates[$col] == $jour->getDate_eng()->format("j/m"))
                            break;
                        $col = $col + 1; 
                    }
                    
                    if($col <= 6) {
                        if($tabDates[$col] == $jour->getDate_eng()->format("j/m")) {
                            $origine = "";
                            if($this->isInTabEffectif($list_effectif, $attrib->getSalarie()) == 0) {
                                $origine = "[".$attrib->getSalarie()->getSection()->getService()->getLib()."] ";
                            }
                            $tabResult2[$numsalarie][0][0] = array($origine ."". $attrib->getSalarie()->getNom() . ' ' . $attrib->getSalarie()->getPrenom() . ' ' . $attrib->getSalarie()->getPrenom2() . ' ' . $attrib->getSalarie()->getPrenom3(), $attrib->getSalarie());
                            $tabResult2[$numsalarie][$col][1] = ($tabResult2[$numsalarie][$col][1]) ? ($tabResult2[$numsalarie][$col][1]."/".$jour->getPoste()->getLib()) : $jour->getPoste()->getLib();
                            $tabResult2[$numsalarie][$col][2] = ($tabResult2[$numsalarie][$col][2]) ? ($tabResult2[$numsalarie][$col][2]."/".$jour->getPlageHoraire()->getLib()) : $jour->getPlageHoraire()->getLib();
                            $tabResult2[$numsalarie][$col][3] = ($tabResult2[$numsalarie][$col][3]) ? ($tabResult2[$numsalarie][$col][3]."/".$jour->getPoste()->getColor()) : $jour->getPoste()->getColor();
                            $test = 'tab2';
                        }
                    }
                }
                if($test == 'tab2')
                    $numsalarie = $numsalarie + 1;
            } // FIN if ($attrib->getSalarie()->getId() == $idsal)
        } // FIN Foreach
        
        $list_salarie = $repository_salarie->get_list_effectif($gs, 'groupe_service');
        $tabResult = $this->interTabObj($list_salarie, $planifier);
        $salarie = $repository_salarie->find($idsal);
        $gs = $repository_groupeservice->find($gs);
        $liste_gs = $repository_groupeservice->findBy(array('exploit' => $gs->getExploit(),'type' => '2'));
        
        /* ---------------------------- PARTIE FORMULAIRE 2 ------------------------------- */
        $new_planifier = new Planifier();
        $form_planifier = $this->createForm(new PlanifierType($gs), $new_planifier);
        
        return $this->render('easydisAPSBundle:Planning:modif_planning.html.twig', array('num_semaine' => $dateFin->add($increment)->format('W'), 'form' => $form_planifier->createView(), 'list_gs' => $liste_gs, 'list_ph'=>$list_ph, 'list_postes'=> $list_postes, 'dispo' => $tabResult, 'tab2' => $tabResult3, 'planning' => $tabResult2, 'tabdates' => $tabDates, 'gs' => $gs, 'salarie' => $salarie, 'dateDebut' => $dateDebut->format("d/m/Y")));
    }
    
    public function update_planning_salarieAction() {
        $em = $this->getDoctrine()->getManager();
        $request = Request::createFromGlobals();
        $repository_planifier = $em->getRepository('easydisAPSBundle:Planifier');
        $repository_poste = $em->getRepository('easydisAPSBundle:Poste');
        $repository_plagehoraire = $em->getRepository('easydisAPSBundle:PlageHoraire');
        $repository_salarie = $em->getRepository('easydisAPSBundle:Salarie');
        
        $gs = $request->request->get('gs');
        $idsal = $request->request->get('idsal');
        $salarie = $repository_salarie->find($idsal);
        $date = $request->request->get('dateDebut');
        $date_current = date_create_from_format ('d/m/Y', $date);
        $interval = new DateInterval('P1D');
        
        $i = 1;
        while($i <= 7) {
            $idposte = $request->request->get('poste_'.$i);
            $id_ph = $request->request->get('ph_'.$i);
            $poste_current = $repository_poste->find($idposte);
            $ph_current = $repository_plagehoraire->find($id_ph);
            
            if($ph_current != null && $poste_current != null) {
                $plannif = $repository_planifier->findOneByCPK($date_current->format('d/m/Y'), $idsal);
                if(2 <= count($plannif)) {
                    foreach($plannif as $element){
                        $em->remove($element);
                    }
                    $em->flush();
                }
                if($plannif != null) {
                    $plannif[0]->setPoste($poste_current);
                    $plannif[0]->setPlageHoraire($ph_current);
                    $plannif[0]->setUser($this->container->get('security.context')->getToken()->getUser());
                    $em->persist($plannif[0]);
                } 
                else 
                {
                    $plannif = new Planifier();
                    $plannif->affectAll($date_current->format('d/m/Y'), $salarie, $ph_current, $poste_current, '1');
                    $plannif->setUser($this->container->get('security.context')->getToken()->getUser());
                    $em->persist($plannif);
                } 
            }
            $date_current->add($interval);
            $i = $i + 1;
        }
        $em->flush();    
        
        return $this->redirect($this->generateUrl('easydisAPSBundle_planning_modifsalaries', array('date' => $date, 'gs' => $gs, 'idsal' => $idsal)));
    }
    
    public function gestion_multiposteAction($gs, $idsal) {
        $em = $this->getDoctrine()->getManager();
        $repository_salarie = $em->getRepository('easydisAPSBundle:Salarie');
        $repository_planifier = $em->getRepository('easydisAPSBundle:Planifier');
        $session = new Session();
        
        $salarie = $repository_salarie->find($idsal);
        $new_planifier = new Planifier();
        $new_planifier_type = new PlanifierType($gs);
        $form = $this->createForm($new_planifier_type, $new_planifier);
       
        $request = $this->get('request');
        if ($request->getMethod() == 'POST') {
            $form->bind($request);
            
            $date = $form["date"]->getData();
            $idph = $form["plagehoraire"]->getData();;
            $plannif = $repository_planifier->findOneByCPK($date, $idsal, $idph);
            if($plannif) {
                $session->getFlashBag()->add('notice', 'Remplacement d\'une affectation déja existante effectué');
                $em->remove($plannif[0]);
                $em->flush();
            } 
            $new_planifier->setSalarie($salarie);
            $new_planifier->setUser($this->container->get('security.context')->getToken()->getUser());
            $new_planifier->setOrdre('1');
            $em->persist($new_planifier);      
            $em->flush();
        }
        return $this->redirect($this->generateUrl('easydisAPSBundle_planning_modifsalaries', array('date' => $date, 'gs' => $gs, 'idsal' => $idsal)));
    }
    
    public function planifier_groupe_salarieAction($gs, $date) {
        $em = $this->getDoctrine()->getManager();
        $repository_salarie = $em->getRepository('easydisAPSBundle:Salarie');
        $repository_poste = $em->getRepository('easydisAPSBundle:Poste');
        $repository_plagehoraire = $em->getRepository('easydisAPSBundle:PlageHoraire');
        $repository_planifier = $em->getRepository('easydisAPSBundle:Planifier');
        $session = new Session();
        
        $list_effectif = $repository_salarie->get_list_effectif($gs, 'groupe_service_visible');
        $list_postes = $repository_poste->get_postes($gs, 'groupe_service');
        $list_plagehoraire = $repository_plagehoraire->get_plagehoraires($gs);
        
        /*-----------------------------*/
        $request = $this->get('request');
        if ($request->getMethod() == 'POST') {
            $interval = new DateInterval('P1D');
            $tabsal = $request->request->get('sals');
            foreach($tabsal as $idsal) {
                $salarie = $repository_salarie->find($idsal);
                $date_debut = $request->request->get('date_debut');
                $date_fin = $request->request->get('date_fin');
                $comport = $request->request->get('comportement');
                
                $date_current = date_create_from_format ('d/m/Y', $date_debut);
                $date_limite = date_create_from_format ('d/m/Y', $date_fin);
                $date_limite->add($interval);
                
                /* ------------------------- */
                $jours_semaine = array('Mon' => 1, 'Tue' => 2,'Wed' => 3,'Thu' => 4,'Fri' => 5,'Sat' => 6,'Sun' => 7);
                while($date_current->format('d/m') != $date_limite->format('d/m')) {
                    $i = $jours_semaine[$date_current->format('D')];
                    $idposte = $request->request->get('poste_'.$i);
                    $id_ph = $request->request->get('ph_'.$i);
                    $poste_current = $repository_poste->find($idposte);
                    $ph_current = $repository_plagehoraire->find($id_ph);
                    $supr = $request->request->get('supr');
                    
                    if($supr == 1) { // gestion del'option de supression des data
                        $plannif = $repository_planifier->findOneByCPK($date_current->format('d/m/Y'), $idsal);
                        foreach($plannif as $element) {
                            $em->remove($element);
                        }
                        $em->flush();
                    } else {
                        if($ph_current != null && $poste_current != null) {
                            $plannif = $repository_planifier->findOneByCPK($date_current->format('d/m/Y'), $idsal);
                            if($comport == '1') {
                                if(2 <= count($plannif)) {
                                    foreach($plannif as $element) {
                                        $em->remove($element);
                                    }
                                    $em->flush();
                                }
                            }
                            if($plannif != null) {
                                $plannif[0]->setPoste($poste_current);
                                $plannif[0]->setPlageHoraire($ph_current);
                                $plannif[0]->setUser($this->container->get('security.context')->getToken()->getUser());
                                $em->persist($plannif[0]);
                            } 
                            else 
                            {
                                $plannif = new Planifier();
                                $plannif->affectAll($date_current->format('d/m/Y'), $salarie, $ph_current, $poste_current, '1');
                                $plannif->setUser($this->container->get('security.context')->getToken()->getUser());
                                $em->persist($plannif);
                            }
                        }
                    }
                    $date_current->add($interval);
                }
            }
            $em->flush();
            if($supr == 1)
                $session->getFlashBag()->add('notice', 'Supression effectuée');
            else
                $session->getFlashBag()->add('success', 'Ajout(s) effectué(s)');
        }
        return $this->render('easydisAPSBundle:Planning:ajouter_planning_groupe.html.twig', array('list_effectif'=>$list_effectif, 'list_postes'=>$list_postes, 'list_ph'=>$list_plagehoraire,'gs'=>$gs, 'dateDebut'=>$date));
    }
    
    /* gestion des formulaires */
    
    public function ajouter_plagehoraireAction($idph) {
        $em = $this->getDoctrine()->getManager();
        $repository_plagehoraire = $em->getRepository('easydisAPSBundle:PlageHoraire');
        
        if($idph != null) {
            $plagehoraire = $repository_plagehoraire->find($idph);
        } else {
            $plagehoraire = new PlageHoraire;
        }
        $form = $this->createForm(new PlagehoraireType(), $plagehoraire);
        $session = new Session();
        
        $request = $this->get('request');
        if ($request->getMethod() == 'POST') {
          $form->bind($request);
          
          if ($form->isValid()) {
            $em->persist($plagehoraire);
            $em->flush();
            
            $plagehoraire = new PlageHoraire;
            $form = $this->createForm(new PlagehoraireType(), $plagehoraire);
            $session->getFlashBag()->set('success', 'Ajout effectué avec succes');
          }
        }

        return $this->render('easydisAPSBundle:Planning:ajouter_plagehoraire.html.twig', array(
          'form' => $form->createView(), 'idph'=>$idph
        ));
    }
    
    public function ajouter_posteAction($idposte) {
        $em = $this->getDoctrine()->getManager();
        $repository_poste = $em->getRepository('easydisAPSBundle:Poste');
        
        if($idposte != null) {
            $poste = $repository_poste->find($idposte);
        } else {
            $poste = new Poste;
        }
        $form = $this->createForm(new PosteType(), $poste);
        $session = new Session();
        
        $request = $this->get('request');
        if ($request->getMethod() == 'POST') {
          $form->bind($request);
          
          if ($form->isValid()) {
            $em->persist($poste);
            $em->flush();
            
            $poste = new Poste;
            $form = $this->createForm(new PosteType(), $poste);
            $session->getFlashBag()->set('success', 'Ajout effectué avec succes');
          }
        }

        return $this->render('easydisAPSBundle:Planning:ajouter_poste.html.twig', array(
          'form' => $form->createView(), 'idposte'=>$idposte
        ));
    }
    
    public function lister_plageshorairesAction($gs, $date) {
        $em = $this->getDoctrine()->getManager();
        $repository_plagehoraire = $em->getRepository('easydisAPSBundle:PlageHoraire');
        
        $list_visible = $repository_plagehoraire->get_plagehoraires($gs);
        
        $request = $this->get('request');
        if ($request->getMethod() == 'POST') {
            $idph = $request->request->get('visible');
 
            if($idph)
                return $this->redirect($this->generateUrl('easydisAPSBundle_planning_ajouter_plagehoraire', array('idph' => $idph)));
    }
        
        return $this->render('easydisAPSBundle:Planning:lister_plageshoraires.html.twig',array('list_visible' => $list_visible));
    }
    
    public function lister_postesAction($gs, $date) {
        $em = $this->getDoctrine()->getManager();
        $repository_poste = $em->getRepository('easydisAPSBundle:Poste');
        
        $list_visible = $repository_poste->get_postes($gs, 'groupe_service');
        
        $request = $this->get('request');
        if ($request->getMethod() == 'POST') {
            $idposte = $request->request->get('visible');
 
            if($idposte)
                return $this->redirect($this->generateUrl('easydisAPSBundle_planning_ajouter_poste', array('idposte' => $idposte)));
        }
        
        return $this->render('easydisAPSBundle:Planning:lister_postes.html.twig',array('list_visible' => $list_visible));
    }
    
    public function lister_effectifAction($gs, $date) {
        $em = $this->getDoctrine()->getManager();
        $session = new Session();
        
        $repository_salarie = $em->getRepository('easydisAPSBundle:Salarie');

        
        $request = $this->get('request');
        if ($request->getMethod() == 'POST') {
            $masquer = $request->request->get('masquer');
            $liste_select = $request->request->get('select');
            foreach($liste_select as $sal) {
                $salarie = $repository_salarie->findOneBy(array('matricule' => $sal));
                if($salarie) {
                    if($masquer) {
                        $salarie->setVisibilite(false);
                    } else {
                        $salarie->setVisibilite(true);
                    }
                    $em->persist($salarie);
                    $em->flush();
                }
            }
        }
        
        $tabResult = array(array());
        
        $tabResult[0][0] = "";
        $tabResult[0][1] = "";
        $tabResult[0][2] = "";
        $tabResult[0][3] = "";
        $tabResult[0][4] = "";
        $tabResult[0][5] = "";
        
        $ligne = 0;
        $list_salarie = $repository_salarie->get_list_effectif($gs, 'groupe_service_vis');
        foreach($list_salarie as $salarie) {
            //Recuperation de la ligne Sous-total niveau groupe de service
            $tabResult[$ligne][0] = $salarie->getMatricule();
            $tabResult[$ligne][1] = $salarie->getNom();
            $tabResult[$ligne][2] = $salarie->getPrenom() . ' ' . $salarie->getPrenom2() . ' ' . $salarie->getPrenom3();
            $tabResult[$ligne][3] = $salarie->getSection()->getLib();
            $tabResult[$ligne][4] = $salarie->getContrat()->getTypeContrat()->getLib();
            $tabResult[$ligne][5] = ($salarie->getVisibilite()==true) ? 'Oui' : 'Non';
            $ligne = $ligne + 1;
        }
        
        return $this->render('easydisAPSBundle:Planning:lister_effectif.html.twig',array('tabResult' => $tabResult, 'gs' => $gs));
    }
    
    public function vision_mensuelleAction() {
        $date = date_create_from_format('d/m/Y', '2/02/2014');
        
        return $this->render('easydisAPSBundle:Planning:planning_vision_mois.html.twig');
    }

    public function recherche_mensuelleAction() {
        $em = $this->getDoctrine()->getManager();
        $repository_salarie = $em->getRepository('easydisAPSBundle:Salarie');
        $repository_poste = $em->getRepository('easydisAPSBundle:Poste');
        $repository_planifier = $em->getRepository('easydisAPSBundle:Planifier');
        $session = new Session();
        
        $date = date_create_from_format('d/m/Y', '2/03/2014');
        $dateFin = date_create_from_format('d/m/Y', '6/03/2014');
        
        
        $tmp = array(
            'id' => 2,
            'title' => 'William DRUGEON',
            'start' => $date->format("Y-m-d\TH:i:sO"),
            'end' => $dateFin->format("Y-m-d\TH:i:sO"),
            'color' => '#03a606');
        
        
        $data = array(array(
            'id' => 1,
            'title' => 'William DRUGEON',
            'start' => time(),
            'end' => time(),
            'color' => '#3a55fa')
            );
        $data[] = $tmp;
        $response = new \Symfony\Component\HttpFoundation\Response(json_encode($data));
        return $response;
    }
    
}

